package com.example.FirstBook.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.FirstBook.MainActivity;
import com.example.FirstBook.adapter.GuideAdapter;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity2 extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    SharedPreferences.Editor editor;
    private ViewPager guidepager;
    private LinearLayout guide_layout;
    List<ImageView> imageViewList;
    List<ImageView> iconlist;
    int[] image = new int[]{R.mipmap.ab1, R.mipmap.ab2, R.mipmap.ab3, R.mipmap.ab4, R.mipmap.ab5};
    GuideAdapter adapter;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide2);
        //隐藏标题栏
        ActionBar actionBar=getSupportActionBar();

        initView();
        //进行harePreferences存储，下次登录，不需要加载引导界面
        editor = getSharedPreferences("data", MODE_PRIVATE).edit();
        editor.putBoolean("guide", true);
        editor.apply();
        initData();
    }

    /*
    初始化适配器数据
     */
    public void initData() {
        imageViewList = new ArrayList<>();
        iconlist = new ArrayList<>();
        for (int i = 0; i < image.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setImageResource(image[i]);
            imageViewList.add(iv);
            ImageView icon = new ImageView(this);
            icon.setImageResource(R.drawable.point_normal);
            LinearLayout.LayoutParams pl = new LinearLayout.LayoutParams(30, 30);
            pl.rightMargin = 30;
            icon.setLayoutParams(pl);
            iconlist.add(icon);
            guide_layout.addView(icon);
        }
        iconlist.get(0).setImageResource(R.drawable.point_select);
        adapter = new GuideAdapter(imageViewList);
        guidepager.setAdapter(adapter);
        guidepager.addOnPageChangeListener(this);
    }
    /*
    初始化控件
     */

    private void initView() {
        guidepager = (ViewPager) findViewById(R.id.guide_pager);
        guide_layout = (LinearLayout) findViewById(R.id.guide_layout);
        button = (Button) findViewById(R.id.button);
        button.setVisibility(View.GONE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GuideActivity2.this, MainActivity.class));
                finish();
            }
        });

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < iconlist.size(); i++) {
            iconlist.get(i).setImageResource(R.drawable.point_normal);
        }
        iconlist.get(position).setImageResource(R.drawable.point_select);
        if (position==4){
            button.setVisibility(View.VISIBLE);
        }else {
            button.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
